package com.ww.springboot.boot.thread.study.base.lock.aqs.demo.lock1;

import com.ww.springboot.boot.thread.study.base.lock.aqs.util.ThreadPrintUtil;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2023-03-17 09:21
 */
public class LockDemo2 {

    public static void main(String[] args) throws InterruptedException {
        MySimpleLock mySimpleLock = new MySimpleLock();
        //创建线程尝试使用简单锁是否可重入
        Thread thread = new Thread(() -> {
            ThreadPrintUtil.sout("开始执行");
            mySimpleLock.lock();
            ThreadPrintUtil.sout("第一次获取锁");
            try {
                mySimpleLock.lock();
                ThreadPrintUtil.sout("第二次获取锁");
            } finally {
                ThreadPrintUtil.sout("释放锁");
                mySimpleLock.unlock();
            }
        });

        thread.start();
        //实验证明 简单锁不允许线程重入
        Thread.sleep(1000);
        mySimpleLock.unlock();
    }
}
